package com.app.biz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.biz.domain.BizProduceSale;
import com.app.common.annotation.Anonymous;
import com.app.common.config.AppConfig;
import com.app.common.core.page.TableDataInfo;
import com.app.common.utils.file.FileUploadUtils;
import com.app.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.BizPmi;
import com.app.biz.service.IBizPmiService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 山东制造业PMIController
 * 
 * @author ZhouS
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/biz/pmi")
public class BizPmiController extends BaseController
{
    @Autowired
    private IBizPmiService bizPmiService;

    /**
     * 查询山东制造业PMI列表
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPmi bizPmi)
    {
        if(StringUtils.isNotBlank(bizPmi.getDates())){
            DateTime datesTimes= DateUtil.parse(bizPmi.getDates(),"yyyy年MM月");
            bizPmi.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        }else{
            AjaxResult.warn("未指定日期！");
        }
        startPage();
        List<BizPmi> list = bizPmiService.selectBizPmiList(bizPmi);
        return getDataTable(list);
    }

    /**
     * 导出山东制造业PMI列表
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:export')")
    @Log(title = "山东制造业PMI", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPmi bizPmi)
    {
        List<BizPmi> list = bizPmiService.selectBizPmiList(bizPmi);
        ExcelUtil<BizPmi> util = new ExcelUtil<BizPmi>(BizPmi.class);
        util.exportExcel(response, list, "山东制造业PMI数据");
    }

    /**
     * 获取山东制造业PMI详细信息
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizPmiService.selectBizPmiByBaseId(baseId));
    }

    /**
     * 新增山东制造业PMI
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:add')")
    @Log(title = "山东制造业PMI", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPmi bizPmi)
    {
        return toAjax(bizPmiService.insertBizPmi(bizPmi));
    }

    /**
     * 修改山东制造业PMI
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:edit')")
    @Log(title = "山东制造业PMI", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPmi bizPmi)
    {
        return toAjax(bizPmiService.updateBizPmi(bizPmi));
    }

    /**
     * 删除山东制造业PMI
     */
    @PreAuthorize("@ss.hasPermi('pmi:pmi:remove')")
    @Log(title = "山东制造业PMI", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizPmiService.deleteBizPmiByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizPmiService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    /**
     * <p>
     *     清空所有的山东制造业PMI数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizPmiService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizPmiService.transferDataInOneClick());
    }

    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String yearMonth = params.get("yearMonth");
            if(StringUtils.isBlank(yearMonth)){
                return AjaxResult.warn("请选择导入的年月");
            }
            List<BizPmi> list = bizPmiService.selectBizPmiListNoParams();
            if(list != null && !list.isEmpty()){
                return AjaxResult.warn("导入之前请先清除历史数据");
            }
            /* 上传文件的路径 */
            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            bizPmiService.importExcel(absoluteExcelPath,yearMonth);
            return AjaxResult.success();
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }
}
